package com.roger.springcloud.nacosconfigdemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.roger.springcloud.nacosconfigdemo.config.CommonProperties;
import com.roger.springcloud.nacosconfigdemo.config.OrderProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author luowenjie
 * @since 2022/1/5 20:08
 */
@RestController
@RequestMapping("/config")
public class ConfigController {

    @Value(value = "${order.pay-timeout-seconds}") // @NacosValue(value = "${order.pay-timeout-seconds}")
    private Integer payTimeoutSeconds;
    @Value(value = "${order.create-frequency-seconds}") // @NacosValue(value = "${order.create-frequency-seconds}")
    private Integer createFrequencySeconds;

    @Autowired
    private CommonProperties commonProperties;

    /**
     * 测试 @Value 注解的属性
     */
    @GetMapping("/test02")
    public Map<String, Object> test02() {
        return new JSONObject().fluentPut("payTimeoutSeconds", payTimeoutSeconds)
                .fluentPut("createFrequencySeconds", createFrequencySeconds)
                .fluentPut("test", "test");
    }

    /**
     * 测试 @Value 注解的属性
     */
    @GetMapping("/common")
    public Map<String, Object> common() {
        return new JSONObject().fluentPut("common", commonProperties.getCommon());
    }
}
